/**
 * Created by billy on 2017/4/25.
 */
var g = require("nodeLib");
var resultList = [];
var lastItemObj = null;
module.exports = class {
	constructor()
	{
		this.add("run", this.test);
	}

	test($data, $succcess, $error, $client)
	{
		var txtContent = g.fs.readFileSync(__projpath("assets/english/self_care.txt")).toString();
// 		console.log(txtContent);
		var rowArr = txtContent.split("\r\n");
// 		console.log(rowArr);
		for (var i = 0; i < rowArr.length; i++)
		{
			var itemList = rowArr[i].split("\t");
// 			console.log(itemList)
			var currId = itemList[0];
			if (currId == "")
			{
				update(lastItemObj, itemList);
			}
			else
			{

				if (itemList[1] == "")
				{
					itemList[1] = lastItemObj.class1;
				}
				if (itemList[2] == "")
				{
					itemList[2] = lastItemObj.class2;
				}
				lastItemObj = createData(itemList);
				if (lastItemObj)
				{
// 					console.log(lastItemObj)
					resultList.push(lastItemObj);
				}
			}

		}
// 		console.log(resultList)
		g.fs.writeFileSync(__projpath("self_care.json"), JSON.stringify(resultList));
		trace("ALL DONE!");
		process.exit();
	}
}
function createData($itemList){
	var d = {};
	d.id = $itemList[0];
	d.type = 0;
	d.class1 = $itemList[1];
	d.class2 = $itemList[2];
	d.title  = $itemList[3];
	d.tools = [];
	d.tools.push($itemList[4]);
	d.way = $itemList[5];
	d.options = [];
	d.options.push($itemList[6]);
	d.target = $itemList[7];
// 	var ageStr = $itemList[7];
// 	ageStr = ageStr.replace("月","");
// 	d.minAge = int(ageStr.split("-")[0]);
// 	d.maxAge = int(ageStr.split("-")[1]);
	return d;
}
function update($obj,$itemList)
{
	var tempTools = $itemList[4];
	var tempWay = $itemList[5];
	var tempOption = $itemList[6];
	var tempTarget = $itemList[7];
	if(tempTools){
		$obj.tools.push(tempTools)
	}
	if(tempWay){
		$obj.way +="</br>"+tempWay;
	}
	if(tempOption){
		$obj.options.push(tempOption)
	}
	if(tempTarget){
		$obj.target += "<br/>" + tempTarget;
	}
}